﻿var status = 0;
var icon = "#fUI/Basic.img/BtMin2/normal/0#";
var iconX = "#fEffect/CharacterEff/1112905/0/1#";
var iconStar = "#fEffect/CharacterEff/1112904/2/1#";
var icon1 = "#fEffect/CharacterEff/1042176/0/0#";
var iconHR = "#fEffect/CharacterEff/1082565/0/0#"
var icon2 = "#fEffect/CharacterEff/1082565/2/0#";
var icon3 = "#fEffect/CharacterEff/1082565/4/0#";
var icon4 = "#fUI/UIWindow2.img/QuestAlarm/BtQ/normal/0#";
var giftContent = Array(
 
  Array("100%力量玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643002, 1)
    )),
  Array("100%敏捷玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643003, 1)
    )),
  Array("100%智力玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643004, 1)
    )),
  Array("100%运气玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643005, 1)
    )),
  Array("100%体力玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643006, 1)
    )),
  Array("100%均衡玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643007, 1)
    )),
  Array("100%破坏玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643106, 1)
    )),
  Array("100%魔力玛瑙石 X 1--只能用于成长型玛瑙戒指", 5, Array(
        Array(2643107, 1)
    )),
  Array("100%强大的力量玛瑙石 X 1--只能用于完成型玛瑙戒指", 10, Array(
        Array(2643116, 1)
    )),
  Array("100%强大的敏捷玛瑙石 X 1--只能用于完成型玛瑙戒指", 10, Array(
        Array(2643117, 1)
    )),
  Array("100%强大的智力玛瑙石 X 1--只能用于完成型玛瑙戒指", 10, Array(
        Array(2643118, 1)
    )),
  Array("100%强大的运气玛瑙石 X 1--只能用于完成型玛瑙戒指", 10, Array(
        Array(2643119, 1)
    )),
  Array("100%强大的体力玛瑙石 X 1--只能用于完成型玛瑙戒指", 10, Array(
        Array(2643120, 1)
    )),
  Array("100%强大的均衡玛瑙石 X 1--只能用于完成型玛瑙戒指", 10, Array(
        Array(2643121, 1)
    ))
);
var giftId = -1;
var gifts = null;
var price = 999;
var column = new Array("装备", "消耗", "设置", "其他", "商城");
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (status == 0 && mode == 0) {
        cm.dispose();
        return;
    }
    if (mode == 1) {
        status++;
    } else {
        status--;
    }
    if (status == 0) {
		var conn = cm.getConnection();
		var pstmt = conn.prepareStatement("select leiji from hypay where accname = '"+cm.getC().getAccountName()+"'");
		var result = pstmt.executeQuery();
		result.next();
		var revenue = result.getString("leiji");
        var text = "";
		text = ""+iconHR+"尊敬的玩家#b#h ##k，欢迎来到#b#e神秘的玛瑙商店！#n#k\r\n";
        //text += "#e#b欢迎来到套装礼包商城，点击可以查看礼包内容哦！\r\n";
		text += ""+icon2+"您当前余额为：#r"+cm.getHyPay(1)+"#k "+"\t\t"+icon2+"累计充值：#r"+revenue+"#k\r\n";
        for(var key in giftContent) {
            text+="#b#L"+key+"#购买【#r#e"+giftContent[key][0]+"#n#b】 #e#d需要"+giftContent[key][1]+"余额#n#b#l#k\r\n";
        }
        cm.sendSimple(text);
    } else if (status == 1) {
        giftId = parseInt(selection);
        price = giftContent[giftId][1];
        gifts = giftContent[giftId][2];
        var text="#r#e"+giftContent[giftId][0]+"#n#b内容：\r\n";
        for(var key in gifts) {
            var itemId = gifts[key][0];
            var itemQuantity = gifts[key][1];
            text+="#i"+itemId+":##b" + (itemId == -1 ? "赠送点券" : "#z" + itemId + "#") + "#k #rx "+itemQuantity+"#k\r\n";
        }
        text+="\r\n#d是否花费#e#r"+price+"#n#d余额购买该礼包？#k";
        cm.sendYesNo(text);
    } else if (status == 2) {
        if (giftId!=-1 && gifts != null) {
            var needslot = new Array(0, 0, 0, 0, 0);
            for (var i in gifts) {
                var type = Math.floor(gifts[i][0] / 1000000);
                if (type == -1) {
                    continue;
                }
                needslot[type - 1] = needslot[type - 1] + 1;
            }
            for (var i = 0; i < 5; i++) {
                if (cm.getSpace(i + 1) < needslot[i]) {
                    cm.sendOk("您的" + column[i] + "栏位空间不足" + needslot[i] + "格，请清理后再来。");
                    cm.dispose();
                    return;
                }
            }
            if (cm.getHyPay(1) < price) {
                cm.sendOk("您的余额不足，请先充值后再购买。");
                cm.dispose();
                return ;
            }
            for(var key in gifts) {
                var itemId = gifts[key][0];
                var itemQuantity = gifts[key][1];
                if (itemId == -1) {
                    cm.gainNX(itemQuantity);
                } else {
                    cm.gainGachaponItem(itemId, itemQuantity,"土豪余额商城",3);
                }
            }
            cm.addHyPay(price);
            cm.sendOk("恭喜您，购买成功！");
			//cm.worldSpouseMessage(0x0F,"[余额商城] 土豪玩家 "+ cm.getChar().getName() +" 从商城里掏走了价值"+price+"的["+giftContent[giftId][0]+"]装备！！！");
            cm.dispose();
        } else {
            cm.sendOk("购买错误！请联系管理员！");
            cm.dispose();
        }
    }
}